#!/usr/bin/python
# -*- coding: utf-8 -*-

""" 容器常用操作 """


t = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9,)
t_abc = ("A", "B", "C",)

l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9,]
l_abc = ["A", "B", "C",]

m = {"a": 0, "b": 1, "c": 2, "d": 3, "e": 4, "f": 5, "g": 6, "h": 7, "i": 8,}
n = {"i": 9.1, "j": 9.3, "k": 9.5,}
m_abc = {"A": "1a!", "B": "2b!", "C": "3c!",}
m_mix = {"A": 1, "0": 2, 3: 3, None: 4, "Hello": 5, u"文字": 6,}


def e1():
	""" in 關鍵字的操作在 tuple, list, dict 上的差異
	"""
	if 3 in t:
		print "3 in t"
	else:
		print "3 not in t"

	if 3 in l:
		print "3 in l"
	else:
		print "3 not in l"

	if 3 in m:
		print "3 in m"
	else:
		print "3 not in m"

	if "c" in m:
		print "\"c\" in m"
	else:
		print "\"c\" not in m"
# ### def e1


def e2():
	""" slice 操作
	"""
	print "t[2:5]>", repr(t[2:5])
	print "t[:5]>", repr(t[:5])
	print "t[2:]>", repr(t[2:])
	print "t[2:-1]>", repr(t[2:-1])

	print "l[2:5]>", repr(l[2:5])
	print "l[:5]>", repr(l[:5])
	print "l[2:]>", repr(l[2:])
	print "l[2:-1]>", repr(l[2:-1])
# ### def e2


def e3():
	""" list.append() 與 list.extend() 的差異
	"""
	l.append(t_abc)
	print "l.append(t_abc)>", l

	l.extend(t_abc)
	print "l.extend(t_abc)>", l
# ### def e3


def e4():
	""" tuple / list unpacking
	"""
	v_1, v_2, v_3, = t_abc
	print "t_abc: v_1=%r, v_2=%r, v_3=%r" % (v_1, v_2, v_3,)

	v_1, v_2, v_3, = l_abc
	print "l_abc: v_1=%r, v_2=%r, v_3=%r" % (v_1, v_2, v_3,)
# ### def e4


def e5():
	""" 附帶索引值的迴圈遊走
	"""
	for idx, v, in enumerate(t_abc):
		print "enumerate(t_abc)> idx=%r, v=%r" % (idx, v,)
	for idx, v, in enumerate(l_abc):
		print "enumerate(l_abc)> idx=%r, v=%r" % (idx, v,)

	for idx, v, in enumerate(m_abc):
		print "enumerate(m_abc)> idx=%r, v=%r" % (idx, v,)
	for idx, v, in enumerate(m_abc.iterkeys()):
		print "enumerate(m_abc.iterkeys())> idx=%r, v=%r" % (idx, v,)
	for idx, v, in enumerate(m_abc.itervalues()):
		print "enumerate(m_abc.itervalues())> idx=%r, v=%r" % (idx, v,)
# ### def e5


def e6():
	""" 字典的迴圈遊走
	"""
	for k in m_mix.iterkeys():
		print "m_mix.iterkeys()> %r" % (k,)

	for v in m_mix.itervalues():
		print "m_mix.itervalues()> %r" % (v,)

	for k, v, in m_mix.iteritems():
		print "m_mix.iteritems()> %r, %r" % (k, v,)
# ### def e6


def e7():
	""" dict 的 update() 方法
	"""
	print "- m>"
	print m
	m.update(n)
	print "- m.update(n)>"
	print m
# ### def e7



e1()
e2()
e3()
e4()
e5()
e6()
e7()



# vim: ts=4 sw=4 foldmethod=marker ai nowrap
